package com.ruoyi.web.controller.warehouse;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.biz.WarehouseService;
import com.ruoyi.system.domain.WarehousingEntry;
import com.ruoyi.system.request.WarehousingEntryAdd;
import com.ruoyi.system.request.WarehousingEntryListRequest;
import com.ruoyi.system.response.WarehousingEntryInfoDetailList;
import com.ruoyi.system.response.WarehousingEntryList;
import com.ruoyi.system.service.IWarehousingEntryInfoService;
import com.ruoyi.system.service.IWarehousingEntryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 入库单Controller
 *
 * @author tanshen
 * @date 2023-08-21
 */
@Controller
@RequestMapping("/warehouse/warehousingEntry")
public class WarehousingEntryController extends BaseController {
    private String prefix = "warehouse/warehousingEntry";

    @Autowired
    private IWarehousingEntryService warehousingEntryService;
    @Autowired
    private WarehouseService warehouseService;
    @Autowired
    private IWarehousingEntryInfoService warehousingEntryInfoService;

    @RequiresPermissions("warehouse:warehousingEntry:view")
    @GetMapping()
    public String warehousingEntry() {
        return prefix + "/warehousingEntry";
    }

    /**
     * 查询入库单列表
     */
    @RequiresPermissions("warehouse:warehousingEntry:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WarehousingEntryListRequest request) {
        startPage();
        List<WarehousingEntryList> list = warehousingEntryService.selectWarehousingEntryList(request);
        return getDataTable(list);
    }

    @GetMapping("/detailList/{entryOrderId}")
    @ResponseBody
    public TableDataInfo detailList(@PathVariable String entryOrderId) {
        startPage();
        List<WarehousingEntryInfoDetailList> list = warehousingEntryInfoService.detailList(entryOrderId);
        return getDataTable(list);
    }

    /**
     * 导出入库单列表
     */
    @RequiresPermissions("warehouse:warehousingEntry:export")
    @Log(title = "入库单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WarehousingEntryListRequest request) {
        List<WarehousingEntryList> list = warehousingEntryService.selectWarehousingEntryList(request);
        ExcelUtil<WarehousingEntryList> util = new ExcelUtil<>(WarehousingEntryList.class);
        return util.exportExcel(list, "入库单数据");
    }

    /**
     * 新增入库单
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        String entryOrderId = "RK" + "08" + DateUtils.parseDateToStr("yyMMddHHmmss", new Date());
        mmap.put("entryOrderId", entryOrderId);
        return prefix + "/add";
    }

    /**
     * 新增保存入库单
     */
    @RequiresPermissions("warehouse:warehousingEntry:add")
    @Log(title = "入库单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WarehousingEntryAdd warehousingEntryAdd) {
        return toAjax(warehouseService.warehousingEntry(warehousingEntryAdd));
    }

    /**
     * 修改入库单
     */
    @RequiresPermissions("warehouse:warehousingEntry:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        WarehousingEntry warehousingEntry = warehousingEntryService.selectWarehousingEntryById(id);
        mmap.put("warehousingEntry", warehousingEntry);
        return prefix + "/edit";
    }

    /**
     * 修改保存入库单
     */
    @RequiresPermissions("warehouse:warehousingEntry:edit")
    @Log(title = "入库单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WarehousingEntry warehousingEntry) {
        return toAjax(warehousingEntryService.updateWarehousingEntry(warehousingEntry));
    }

    /**
     * 删除入库单
     */
    @RequiresPermissions("warehouse:warehousingEntry:remove")
    @Log(title = "入库单", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(warehousingEntryService.deleteWarehousingEntryByIds(ids));
    }
}
